//
//  HBPopView.m
//  HBActionSheet
//
//  Created by 韩冰 on 2017/10/23.
//  Copyright © 2017年 韩冰. All rights reserved.
//

#import "HBPopView.h"
#import "HBWindow.h"
#import "HBCommon.h"

@implementation HBPopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if ( self )
    {
        [self setup];
    }
    
    return self;
}
- (instancetype)init{
    self = [super init];
    
    if ( self )
    {
        [self setup];
    }
    
    return self;
    
}
- (void)setup{
    self.type = HBPopViewTypeAlert;
    self.animationDuration = 0.3f;
    self.attachedView = [HBWindow sharedWindow].attachView;
}
- (void)setTouchBgWildToHide:(BOOL)touchBgWildToHide{
    
    _touchBgWildToHide = touchBgWildToHide;
    [[HBWindow sharedWindow] setTouchWildToHide: _touchBgWildToHide];
    
}
- (void)setType:(HBPopViewType)type
{
    _type = type;
    
    switch (type)
    {
        case HBPopViewTypeAlert:
        {
            self.showAnimation = [self alertShowAnimation];
            self.hideAnimation = [self alertHideAnimation];
            break;
        }
        case HBPopViewTypeSheet:
        {
            self.showAnimation = [self sheetShowAnimation];
            self.hideAnimation = [self sheetHideAnimation];
            break;
        }
        case HBPopViewTypeCustom:
        {
            self.showAnimation = [self customShowAnimation];
            self.hideAnimation = [self customHideAnimation];
            
            break;
        }
            
        default:
            break;
    }
}
- (BOOL)visible
{
    if ( self.attachedView )
    {
        return !self.attachedView.HB_dimBackgroundView.hidden;
    }
    
    return NO;
}
- (void)setAnimationDuration:(NSTimeInterval)animationDuration
{
    _animationDuration = animationDuration;
    
    self.attachedView.HB_dimAnimationDuration = animationDuration;
}
- (void)show
{
    [self showWithBlock:nil];
}

- (void)showWithBlock:(HBPopupCompletionBlock)block
{
    if ( block )
    {
        self.showCompletionBlock = block;
    }
    
    if ( !self.attachedView )
    {
        self.attachedView = [HBWindow sharedWindow].attachView;
    }
    [self.attachedView HB_showDimBackground];
    
    HBPopupBlock showAnimation = self.showAnimation;
    
    NSAssert(showAnimation, @"show animation must be there");
    
    showAnimation(self);
    
//    if ( self.withKeyboard )
//    {
//        [self showKeyboard];
//    }
}

- (void)hide
{
    [self hideWithBlock:nil];
}

- (void)hideWithBlock:(HBPopupCompletionBlock)block
{
    if ( block )
    {
        self.hideCompletionBlock = block;
    }
    
    if ( !self.attachedView )
    {
        self.attachedView = [HBWindow sharedWindow].attachView;
    }
    [self.attachedView HB_hideDimBackground];
    
//    if ( self.withKeyboard )
//    {
//        [self hideKeyboard];
//    }
//
    HBPopupBlock hideAnimation = self.hideAnimation;
    
    NSAssert(hideAnimation, @"hide animation must be there");
    
    hideAnimation(self);
}

- (HBPopupBlock)alertShowAnimation
{
    WEAKSELF;
    HBPopupBlock block = ^(HBPopView *popupView){
        STRONGSELF;
        
        if ( !strongSelf.superview )
        {
            [strongSelf.attachedView.HB_dimBackgroundView addSubview:strongSelf];
            strongSelf.center = strongSelf.attachedView.center;
            [strongSelf layoutIfNeeded];
        }
        
        strongSelf.layer.transform = CATransform3DMakeScale(1.2f, 1.2f, 1.0f);
        strongSelf.alpha = 0.0f;
        
        [UIView animateWithDuration:strongSelf.animationDuration
                              delay:0.0 options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             
                             strongSelf.layer.transform = CATransform3DIdentity;
                             strongSelf.alpha = 1.0f;
                             
                         } completion:^(BOOL finished) {
                             
                             if ( strongSelf.showCompletionBlock )
                             {
                                 strongSelf.showCompletionBlock(strongSelf, finished);
                             }
                         }];
    };
    
    return block;
}

- (HBPopupBlock)alertHideAnimation
{
   WEAKSELF
    HBPopupBlock block = ^(HBPopView *popupView){
        STRONGSELF
        
        [UIView animateWithDuration:strongSelf.animationDuration
                              delay:0
                            options: UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             
                             strongSelf.alpha = 0.0f;
                             
                         }
                         completion:^(BOOL finished) {
                             
                             if ( finished )
                             {
                                 [strongSelf removeFromSuperview];
                             }
                             
                             if ( strongSelf.hideCompletionBlock )
                             {
                                 strongSelf.hideCompletionBlock(strongSelf, finished);
                             }
                             
                         }];
    };
    
    return block;
}

- (HBPopupBlock)sheetShowAnimation
{
    WEAKSELF
    HBPopupBlock block = ^(HBPopView *popupView){
       STRONGSELF;
        
        if ( !strongSelf.superview )
        {
            [strongSelf.attachedView.HB_dimBackgroundView addSubview:strongSelf];
            strongSelf.centerX = strongSelf.attachedView.centerX;
            strongSelf.bottom = strongSelf.attachedView.bottom + strongSelf.attachedView.height;
            [strongSelf layoutIfNeeded];
        }
        
        [UIView animateWithDuration:strongSelf.animationDuration
                              delay:0
                            options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             strongSelf.bottom = strongSelf.attachedView.bottom;
                             [strongSelf.superview layoutIfNeeded];
                             
                         }
                         completion:^(BOOL finished) {
                             
                             if ( strongSelf.showCompletionBlock )
                             {
                                 strongSelf.showCompletionBlock(strongSelf, finished);
                             }
                             
                         }];
    };
    
    return block;
}

- (HBPopupBlock)sheetHideAnimation
{
   WEAKSELF
    HBPopupBlock block = ^(HBPopView *popupView){
        STRONGSELF
        
        [UIView animateWithDuration:strongSelf.animationDuration
                              delay:0
                            options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             strongSelf.bottom = strongSelf.attachedView.bottom + strongSelf.attachedView.height;
                             [strongSelf.superview layoutIfNeeded];
                             
                         }
                         completion:^(BOOL finished) {
                             
                             if ( finished )
                             {
                                 [strongSelf removeFromSuperview];
                             }
                             
                             if ( strongSelf.hideCompletionBlock )
                             {
                                 strongSelf.hideCompletionBlock(strongSelf, finished);
                             }
                             
                         }];
    };
    
    return block;
}

- (HBPopupBlock)customShowAnimation
{
    WEAKSELF
    HBPopupBlock block = ^(HBPopView *popupView){
       STRONGSELF;
        
        if ( !strongSelf.superview )
        {
            [strongSelf.attachedView.HB_dimBackgroundView addSubview:self];
            strongSelf.center = strongSelf.attachedView.center;
            strongSelf.alpha = 0;
//            [strongSelf mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.center.equalTo(strongSelf.attachedView).centerOffset(CGPointMake(0, -strongSelf.attachedView.bounds.size.height));
//            }];
            [strongSelf layoutIfNeeded];
        }
        
        [UIView animateWithDuration:strongSelf.animationDuration
                              delay:0
             usingSpringWithDamping:0.8
              initialSpringVelocity:1.5
                            options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             strongSelf.alpha = 1.0;
//                             strongSelf.center =
//                             [strongSelf mas_updateConstraints:^(MASConstraintMaker *make) {
//                                 make.center.equalTo(strongSelf.attachedView).centerOffset(CGPointMake(0, strongSelf.withKeyboard?-216/2:0));
//                             }];
                             
                             [strongSelf.superview layoutIfNeeded];
                             
                         } completion:^(BOOL finished) {
                             
                             if ( strongSelf.showCompletionBlock )
                             {
                                 strongSelf.showCompletionBlock(self, finished);
                             }
                             
                         }];
    };
    
    return block;
}

- (HBPopupBlock)customHideAnimation
{
    WEAKSELF;
    HBPopupBlock block = ^(HBPopView *popupView){
        STRONGSELF
        
        [UIView animateWithDuration:0.3
                              delay:0
                            options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
                         animations:^{
                             strongSelf.alpha = 0;
                             [strongSelf.superview layoutIfNeeded];
                             
                         } completion:^(BOOL finished) {
                             
                             if ( finished )
                             {
                                 [strongSelf removeFromSuperview];
                             }
                             
                             if ( strongSelf.hideCompletionBlock )
                             {
                                 strongSelf.hideCompletionBlock(strongSelf, finished);
                             }
                         }];
    };
    
    return block;
}

@end
